<?php

namespace backend\controllers;

use common\logic\RentingLogic;
use Yii;
use common\models\RentingModel;
use common\models\RentingMarkModel;
use backend\models\SearchRentingModel;
use yii\web\NotFoundHttpException;
use yii\helpers\ArrayHelper;

/**
 * HouseController implements the CRUD actions for HouseModel model.
 */
class RentingController extends BaseController
{


    public function actionAsyncImage($id)
    {
        if (in_array($id, ['adv', 'banner']))
            // 商品ID
            $id = Yii::$app->request->post('goods_id');

        // $p1 $p2是我们处理完图片之后需要返回的信息，其参数意义可参考上面的讲解
        $p1 = $p2 = [];


        // 如果没有商品图或者商品id非真，返回空
        if (empty($_FILES['Banner']['name']) || empty($_FILES['Banner']['name']['banner']) || !$id) {
            echo '{}';

            return;
        }

        // 循环多张商品banner图进行上传和上传后的处理
        for ($i = 0; $i < count($_FILES['housemodel']['name'][$id]); $i++) {
            // 上传之后的商品图是可以进行删除操作的，我们为每一个商品成功的商品图指定删除操作的地址
            $url = '/banner/delete';

            // 调用图片接口上传后返回的图片地址，注意是可访问到的图片地址哦
            $imageUrl = '';


            $key = 0;


            $p1[$i] = $imageUrl;
            $p2[$i] = ['url' => $url, 'key' => $key];
        }


        // 返回上传成功后的商品图信息
        echo json_encode([
            'initialPreview' => $p1,
            'initialPreviewConfig' => $p2,
            'append' => true,
        ]);

        return;
    }

    /**
     * Lists all HouseModel models.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new SearchRentingModel();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->sort = false;

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single HouseModel model.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new HouseModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     *
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new RentingModel();
        $model->is_adv = 0;
        $model->is_banner = 0;
        $model->is_recommend = 0;
        $model->recommend_sort=0;
        $model->longitude=0;
        $model->latitude=0;
        $model->creater = Yii::$app->user->getId();

        $post = Yii::$app->request->post();

        if(isset($post['RentingModel']))
        {
            $model->house_mark=$post['RentingModel']['house_mark'];
        }

        if ($model->load($post) && RentingLogic::instance()->houseSave($model)) {
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing HouseModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if(!empty($model->building_type)){
            $type = $model->building_type;
            $model->building_type = explode(',',$type);
        }

        $post = Yii::$app->request->post();
        unset($post['RentingModel']['banner'],$post['RentingModel']['adv']);

        if(isset($post['RentingModel'])) {
            $model->house_mark = $post['RentingModel']['house_mark'];
        }

        if ($model->load($post) && RentingLogic::instance()->houseSave($model)) {
            return $this->redirect(['index']);
        } else {
            $model->house_mark=RentingMarkModel::findAll(['house_id'=>$id]);
            if(!empty($model->house_mark)){
                $marks= $model->house_mark;
                $model->house_mark=ArrayHelper::map($marks,'id','mark_id');
            }
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }


    /**
     * Deletes an existing HouseModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        RentingMarkModel::deleteAll(['house_id'=>$id]);

        return $this->redirect(['index']);
    }



    /**
     * Finds the HouseModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param integer $id
     *
     * @return RentingModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = RentingModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
	
	
}
